﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Obligatorio.DataAccess;

/// <summary>
/// Summary description for StockPresenter
/// </summary>
public class StockPresenter
{
	#region Fields
	IStockView _view;
	#endregion
	public StockPresenter(IStockView _view)
	{
		this._view = _view;
	}

    public void GetBranches()
    {
        using (var data = new ObligatorioDotNetEntities())
        {
            this._view.Branches = data.Branches.ToList();
        }
    }

    public void GetProducts()
    {
        using (var data= new ObligatorioDotNetEntities())
        {
            this._view.BranchProducts = data.SP_GetProductsByBranch(this._view.SelectedBranch).ToList();
        }
    }

    public void GetAllProducts()
    {
        using (var data = new ObligatorioDotNetEntities())
        {
            this._view.Products = data.Products.ToList();
        }
    }

    public void GetProviders()
    {
        using (var data = new ObligatorioDotNetEntities())
        {
            this._view.Providers = data.Providers.ToList();
        }
    }

    public void AddStock()
    {
        using (var data= new ObligatorioDotNetEntities())
        {
            var product = data.ProductStocks.Where(ps => ps.BranchId == this._view.SelectedBranch &&
                ps.ProductId == this._view.Product).SingleOrDefault();
            if (product != null)
            {
                product.Quantity += this._view.Quantity;
            }
            else
            {
                product = new ProductStock();
                product.Quantity = this._view.Quantity;
                product.ProductId = this._view.Product;
                product.BranchId = this._view.SelectedBranch;
                data.ProductStocks.AddObject(product);
            }
            data.SaveChanges();
        }
    }
}